layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };
    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyWage/getDm");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult, function( index, val ) {
        $("#dm").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
    } );
    form.render('select');

    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            {field: 'xm', align: "center", sort: true, title: '姓名',fixed: 'left',width:80},
            {field: 'flag', align: "center", sort: true, title: '月份'},
            {field: 'dm', align: "center", sort: true, title: '店名',width:110},
            // {field: 'rzsj', align: "center", sort: true, title: '入职时间'},
            {field: 'yhk', align: "center", sort: false, title: '银行卡',width:120},
             // {field: 'zw', align: "center", sort: true, title: '职务'},
            {field: 'gz', align: "center", sort: false, title: '基本工资',totalRow:true,width:90},
            {field: 'yfhj', align: "center", sort: false, title: '应发合计',totalRow:true,width:90},
            {field: 'kkhj', align: "center", sort: false, title: '扣款合计',totalRow:true,width:90},
            {field: 'sfhj', align: "center", sort: false, title: '实发合计',totalRow:true,width:90},
            {field: 'days', align: "center", sort: false, title: '本月',width:60},
            {field: 'cqts', align: "center", sort: false, title: '出勤',width:60},
            {field: 'gxts', align: "center", sort: false, title: '公休',width:60},
            {field: 'qqj', align: "center", sort: false, title: '全勤奖',width:80},
            {field: 'sxts', align: "center", sort: false, title: '三薪天数',width:90},
            {field: 'tbgz', align: "center", sort: false, title: '填补工资',width:90},
            {field: 'cdkk', align: "center", sort: false, title: '迟到',width:60},
            {field: 'ldkk', align: "center", sort: false, title: '缺卡',width:60},
            {field: 'hp', align: "center", sort: false, title: '货赔',width:60},
            {field: 'qtkk', align: "center", sort: false, title: '其它扣款',width:90},
            // {field: 'bh', align: "center", sort: true, title: 'ID'},
            {field: 'bz', align: "center", sort: false, title: '备注',width:60},
            // {field: 'createrName', align: "center", sort: true, title: '发布者'},
            // {field: 'createTime', align: "center", sort: true, title: '创建时间'},
            //{align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['dm'] = $("#dm").val();
        queryData['status'] = $("#status").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加通知
     */
    Notice.openAddNotice = function () {
        func.open({
            height: 420,
            title: '添加通知',
            content: Feng.ctxPath + '/notice/notice_add',
            tableId: Notice.tableId
        });
    };

    /**
     * 点击编辑通知
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.confirmOk = function () {
        var checkRows = table.checkStatus(Notice.tableId);
        var data = checkRows.data;
        var ids="";
        if (checkRows.data.length === 0) {
            Feng.error("请选择要确认的数据");
        }else{
           for (var i in data){
               ids+=data[i].id+",";
           }
            // func.open({
            //     height: 420,
            //     title: '确认',
            //     content: Feng.ctxPath + "/btyWage/confirmOk/" + ids,
            //     tableId: Notice.tableId
            // });
            var ajax = new $ax(Feng.ctxPath + "/btyWage/confirmOk", function () {
                Feng.success("确认成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("确认失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids",ids);
            ajax.start();
        }

    };
    /**
     * 点击批量取消
     * @param data
     */
    Notice.confirmQx = function () {
        var checkRows = table.checkStatus(Notice.tableId);
        var data = checkRows.data;
        var ids="";
        if (checkRows.data.length === 0) {
            Feng.error("请选择要取消审核的数据");
        }else{
            for (var i in data){
                ids+=data[i].id+",";
            }
            var ajax = new $ax(Feng.ctxPath + "/btyWage/confirmQx", function () {
                Feng.success("取消审核成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("取消审核失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids",ids);
            ajax.start();
        }

    };
    Notice.onEditNotice = function (data) {
        func.open({
            height: 420,
            title: '修改通知',
            content: Feng.ctxPath + "/notice/notice_update/" + data.noticeId,
            tableId: Notice.tableId
        });
    };

    /**
     * 点击删除通知
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onDeleteNotice = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/notice/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("noticeId", data.noticeId);
            ajax.start();
        };
        Feng.confirm("是否删除通知 " + data.title + "?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/btyWage/list',
        totalRow:true,	//开启合计行
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        limit: 20,
        cols: Notice.initColumn(),
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Notice.openAddNotice();
    });
    // 确认按钮点击事件
    $('#btnOk').click(function () {
        Notice.confirmOk();
    });
    // 取消审核按钮点击事件
    $('#btnQx').click(function () {
        Notice.confirmQx();
    });

    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'delete') {
            Notice.onDeleteNotice(data);
        }
    });
});
